package edu.ynmd.cms.action;

import edu.ynmd.cms.model.Carousel;
import edu.ynmd.cms.model.News;
import edu.ynmd.cms.model.Singlepagelist;
import edu.ynmd.cms.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("manage")
public class AdminAction {

    @Autowired
    public ManageService manageService;

    @PostMapping("addNews")
    @ResponseBody
    public boolean addNews(@RequestBody News news) throws Exception{
        news.setPbdate(new Timestamp(System.currentTimeMillis()));
        return manageService.addNews(news);
    }

    @PostMapping("deleteNews")
    @ResponseBody
    public boolean deleteNews(@RequestBody News news) throws Exception{
        return manageService.deleteNews(news.getNewid());
    }

    @PostMapping("updateNews")
    @ResponseBody
    public boolean updateNews(@RequestBody News news) throws Exception{
        return manageService.updateNews(news);
    }

    @PostMapping("getSingleNews")
    @ResponseBody
    public News getSingleNews(@RequestBody News news) throws Exception{
        return manageService.getNews(news.getNewid());
    }

    @GetMapping("getNewsList")
    @ResponseBody
    public List<News> getNewsList() throws Exception{
        return manageService.getNewsList();
    }

    @PostMapping("uploadFile")
    @ResponseBody
    public Map uploadFile(MultipartFile file) throws Exception{
        Map m=new HashMap();

        String filename=file.getOriginalFilename();
        System.out.printf("接收到的文件名是:"+filename);
        //获得文件的后缀
        int index=filename.lastIndexOf(".");
        String sufixname=filename.substring(index);
        String savefilename=String.valueOf(System.currentTimeMillis())+sufixname;

        //检测文件夹的存在
        String savepath="D:\\JAVA EE\\springbootupload\\";
        File f=new  File(savepath);
        if (!f.exists()){
            f.mkdir();
        }
        //将文件保存到文件夹
        file.transferTo(new File(savepath+savefilename));
        m.put("pic",savefilename);
        return m;
    }

    @RequestMapping("/ckeditorUpload")
    @ResponseBody
    public String ckeditorUpload(@RequestParam("upload") MultipartFile file, String CKEditorFuncNum) throws Exception {
        if (!file.isEmpty()) {
            String finename=file.getOriginalFilename();
            String suffixname=file.getOriginalFilename().substring(finename.lastIndexOf("."));
            finename=String.valueOf(System.currentTimeMillis())+suffixname;
            String filepath="D:/JAVA EE/springbootupload/";
            File tf=new File(filepath);
            if(!tf.exists()){
                tf.mkdir();
            }
            String savefile=filepath+finename;
            try {
                file.transferTo(new File(savefile));
                String url="http://localhost/"+finename;
                return "{\"uploaded\":1,\"fileName\":\""+savefile+"\",\"url\":\"" + url + "\"}";

            } catch (IOException e) {
                e.printStackTrace();
                return "{\"uploaded\":0,\"error\":{\"message\":\"upload file is not success!\"}}";
            } catch (IllegalStateException e) {
                e.printStackTrace();
                return "{\"uploaded\":0,\"error\":{\"message\":\"upload file is not success!\"}}";
            }

        }

        return "{\"uploaded\":0,\"error\":{\"message\":\"upload file is not success!\"}}";

    }


    @PostMapping("addCarousel")
    @ResponseBody
    public boolean addCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.addCarousel(carousel);
    }

    @GetMapping("getCarouselList")
    @ResponseBody
    public List<Carousel> getCarouselList() throws Exception{
        return manageService.getCarouselList();
    }

    @PostMapping("getCarousel")
    @ResponseBody
    public Carousel getCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.getCarousel(carousel.getCarouselid());
    }

    @PostMapping("deleteCarousel")
    @ResponseBody
    public boolean deleteCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.deleteCarousel(carousel.getCarouselid());
    }


    @PostMapping("updateCarousel")
    @ResponseBody
    public boolean updateCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.updateCarousel(carousel);
    }

    @PostMapping("addSinglepagelist")
    @ResponseBody
    public boolean addSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.addSinglepagelist(singlepagelist);
    }

    @GetMapping("getSinglepagelistList")
    @ResponseBody
    public List<Singlepagelist> getSinglepagelistList() throws Exception{
        return manageService.getSinglepagelistList();
    }


    @PostMapping("getSinglepagelist")
    @ResponseBody
    public Singlepagelist getSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.getSinglepagelist(singlepagelist.getSinglepageid());
    }

    @PostMapping("deleteSinglepagelist")
    @ResponseBody
    public boolean deleteSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.deleteSinglepagelist(singlepagelist.getSinglepageid());
    }


    @PostMapping("updateSinglepagelist")
    @ResponseBody
    public boolean updateSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.updateSinglepagelist(singlepagelist);
    }


}
